// 考试
const router = require('express').Router()
const db = require('../db')
const toWhere = require('../utils').toWhere
// 得到试卷考试内容
/*
router.get('/getPaperTest', (req, res) => {
    let { role, id } = req.query.user
    // let sql = 'select * from qst_single;'
    let sql = 'select * from qst_more'
    db.query(sql, [], result => {
        res.json({
            code: 0,
            // single: result
            qst_more: result
        })
    })
})*/

// 考试记录表保存
router.post('/record/save', (req, res) => {
    // console.log('req.body', req.body)
    let data = req.body
    data.studentId = parseInt(req.query.user.id)
    console.log('req.body++++++++++++++++++++======', req.body)
    if (typeof data.content !== 'string') data.content = JSON.stringify(data.content);
    if (data.recordId) {
        db.query(`select isSubmit from record WHERE id = ${data.recordId} `, [], result => {
            // console.log('isSubmit', isSubmit)
            if (result[0].isSubmit) {
                res.json({ code: 403, msg: 'eror' })
            } else {
                let id = data.recordId
                delete data.recordId
                db.query(`UPDATE record set ?  WHERE id = ${id}`, data, result => {
                    res.json({ code: 0, msg: 'success' })
                })
            }
        })

    } else {
        delete data.recordId
        db.query('INSERT INTO record set ? ', data, result => {
            res.json({ code: 0, recordId: result.insertId })
        })
    }
})

// 得到考试记录表, 必须携带参数
router.get('/getRecordList', (req, res) => {
    delete req.query.user
    if (JSON.stringify(req.query) == '{}') {
        res.json({ code: 403, msg: 'no params' })
        return;
    }
    let sql = toWhere('record_view', req.query);
    db.query(sql[0], sql[1], data => {
        res.json({
            code: 0,
            data
        })
    })
})

// 得到解析
router.post('/record/analysis', (req, res) => {
    console.log('req.body', req.body)
})
// 得到某个学生具体的考试记录信息
router.post('/record/getDetail', (req, res) => {
    console.log('req.body', req.body)
})
// function 
function getSingle() {
    let sql = 'SELECT * FROM qst_single WHERE id in() '
    db.query(addsql, params, result => {
        res.json({
            code: 0,
            score: 50,
            result
        })
    })
}

module.exports = router